/*
 * ShiftDialog.java
 * 
 * Copyright (c) 2008, Marshall Scorcio, Ralph Holzmann, 
 * Stewart Sweet, Tyler Hoersch, Jeffery Van Handel
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * * Neither the name of the University of Wisconsin Oshkosh nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */

import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
import java.text.*;

public class ShiftDialog extends JDialog implements ActionListener
{
    public final static int CONFIRM_CLOSE = 1;
    public final static int CANCEL_CLOSE = 0;
    private final String[] POSSIBLE_NUMBERS = { "1", "2", "3", "4", "5",
                                                "5", "6", "7", "8", "9", "10" };
    
    private int closeCode;
    
    private SimpleDateFormat dateFormat;
    
    private JPanel northPane;
    private JPanel southPane;
    
    private GridLayout gridLayout;
    
    private JComboBox numberOfShiftsBox;
    
    private JLabel numberOfShiftsLabel;
    private JLabel startTimeLabel;
    private JLabel endTimeLabel;
    
    private JSpinner endTimeSpinner;
    private JSpinner startTimeSpinner;
    
    private SpinnerDateModel startTimeModel;
    private SpinnerDateModel endTimeModel;
    
    private JSpinner.DateEditor startTimeEditor;
    private JSpinner.DateEditor endTimeEditor;
    
    private JButton cancel;
    private JButton confirm;
    
    private Box cancelConfirmBox;
    
    public ShiftDialog(JFrame parent)
    {
        super(parent, true);
        dateFormat  = new SimpleDateFormat("hh:mm a");
        
        gridLayout = new GridLayout(0,4);
        
        northPane = new JPanel(new FlowLayout());
        southPane = new JPanel(gridLayout);
        
        numberOfShiftsBox = new JComboBox(POSSIBLE_NUMBERS);
        
        numberOfShiftsLabel = new JLabel("Add additional Shifts: ");
        startTimeLabel = new JLabel("1. Start Time: ");
        endTimeLabel = new JLabel(" End Time: ");
        
        startTimeModel = new SpinnerDateModel(new Date(), null, null, Calendar.HOUR_OF_DAY);
        startTimeSpinner = new JSpinner(startTimeModel);
        startTimeEditor = new JSpinner.DateEditor(startTimeSpinner, "hh:mm a");
        
        endTimeModel = new SpinnerDateModel(new Date(),null,null,Calendar.HOUR_OF_DAY);
        endTimeSpinner = new JSpinner(endTimeModel);
        endTimeEditor = new JSpinner.DateEditor(endTimeSpinner, "hh:mm a");
        
        cancel = new JButton("Cancel");
        confirm = new JButton("Confirm");
        
        cancelConfirmBox = Box.createHorizontalBox();
        cancelConfirmBox.add(Box.createHorizontalGlue());
        cancelConfirmBox.add(cancel);
        cancelConfirmBox.add(confirm);
    
        cancel.addActionListener(this);
        confirm.addActionListener(this);
        numberOfShiftsBox.addActionListener(this);
        
        northPane.add(numberOfShiftsLabel);
        northPane.add(numberOfShiftsBox);
        
        startTimeSpinner.setEditor(startTimeEditor);
        endTimeSpinner.setEditor(endTimeEditor);
        
        southPane.add(startTimeLabel);
        southPane.add(startTimeSpinner);
        southPane.add(endTimeLabel);
        southPane.add(endTimeSpinner);
        
        setLayout(new BorderLayout());
        
        add(northPane, BorderLayout.NORTH);
        add(southPane, BorderLayout.CENTER);
        add(cancelConfirmBox, BorderLayout.SOUTH);
        
        setTitle("Add Shifts");
        setSize(350,125);
        setCloseCode(CANCEL_CLOSE);
        pack();
    }
    public String[] toStringArray()
    {
        int numberOfShifts = Integer.parseInt((String)numberOfShiftsBox.getSelectedItem());
        Component[] components = southPane.getComponents();
        String[] toString = new String[numberOfShifts];
        
        for(int i = 0; i < numberOfShifts; i++)
        {
            JSpinner start = (JSpinner)components[(i*4) + 1];
            JSpinner end = (JSpinner)components[(i*4)+3];
                  
            toString[i] = dateFormat.format(start.getValue()) + " - " + dateFormat.format(end.getValue());
        }
        return toString;
    }
    public void setCloseCode(int code)
    {
        closeCode = code;
    }
    public int getCloseCode()
    {
        return closeCode;
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource().equals(numberOfShiftsBox))
        {
            int numberOfShifts = Integer.parseInt((String)numberOfShiftsBox.getSelectedItem());
            gridLayout = new GridLayout(numberOfShifts,4);
            southPane.removeAll();
            
            for(int i = 0; i < numberOfShifts; i++)
            {
                SpinnerDateModel startModel = new SpinnerDateModel(new Date(), null, null, Calendar.HOUR_OF_DAY);
                JSpinner spinner = new JSpinner(startModel);
                JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner, "hh:mm a");
                spinner.setEditor(editor);
                                
                SpinnerDateModel endModel = new SpinnerDateModel(new Date(), null, null, Calendar.HOUR_OF_DAY);
                JSpinner spinner2 = new JSpinner(endModel);
                JSpinner.DateEditor editor2 = new JSpinner.DateEditor(spinner2, "hh:mm a");
                spinner2.setEditor(editor2);
                                    
                southPane.add(new JLabel(i+1 + ". Start Time: "));
                southPane.add(spinner);
                southPane.add(new JLabel(" End Time: "));
                southPane.add(spinner2);
                
                pack();
            }
        }
        else if(e.getSource().equals(confirm))
        {
            setCloseCode(CONFIRM_CLOSE);
            setVisible(false);
        }
        else if(e.getSource().equals(cancel))
        {
            setCloseCode(CANCEL_CLOSE);
            setVisible(false);
        }
    }
}
